





//
//  ZAddressListViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/6/10.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZAddressListViewController.h"
#import "AddressCell.h"
#import "ZAddAddressViewController.h"
@interface ZAddressListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ZAddressListViewController
#define cellIdentifier @"address"
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 95.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setItemWithTitle:@"选择收货地址" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.dataArray = [NSMutableArray array];
//    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"添加" Image:@"" selectedIMG:@"" tartget:self action:@selector(addAddress)];
    
    [self.view addSubview:self.tableView];
    
    [self loadUserAddress];
    // Do any additional setup after loading the view.
}
///添加地址
-(void)addAddress{

//    YYLog(@"添加地址");
    ZAddAddressViewController *add = [ZAddAddressViewController new];
    [self.navigationController pushViewController:add animated:YES];
    
}

#pragma mark - 收货地址
///获取用户收货地址列表
-(void)loadUserAddress{
    
    NSDictionary *dict = @{@"pageIndex":@"1",@"pageSize":@"10",@"userInfo.id":[UserInfoSave shareInstance].uid?:@""};
    WeakStament(ws);
    [ZNetworkService postRequestWithBaseUrl:kAddressUrl setParameters:dict success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
//            NSDictionary *dict = [arr firstObject];
            for (NSDictionary *dict in arr) {
                CommonModel *model = [CommonModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [self.tableView reloadData];
            
//            self.addressId = model.ID;
//            YYLog(@"%@",self.addressId);
            ///就一个对象
//            [ws setUIWithModel:model];
            //            [self.addressArray addObject:model];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.dataArray[indexPath.row];
    NSString *add = [NSString stringWithFormat:@"%@%@%@%@",model.province,model.city,model.area,model.address];
    AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.nameLab.text = [NSString stringWithFormat:@"收货人:%@",model.name];
    cell.phoneLab.text = model.phone;
    
    cell.addLab.text = [NSString stringWithFormat:@"收货地址:%@",add];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.dataArray[indexPath.row];
    if (self.block) {
        self.block(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
